<?php

namespace App\Admin\Controllers;

use App\Constant\Enum;
use App\Models\Admodel;
use App\Models\Adplatforms;
use App\Models\Adtype;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class AdmodelController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {

        return Grid::make(new Admodel(), function (Grid $grid) {

            $grid->model()->with('adtype');
            $grid->column('id')->sortable();
            $grid->column('adtype_id')->display(function () {
                $text = '<i>-</i>';
                $type = $this->adtype;
                if ($type) {
                    $text = "<a href='adtypes?id={$type->id}'>{$type->name}</a>";
                }
                return $text;
            });
            $grid->column('adplatform_id')->display(function () {
                $text = '<i>-</i>';
                $platform = $this->adplatform;
                if ($platform) {
                    $text = "<a href='adplatform?id={$platform->id}'>{$platform->name}</a>";
                }
                return $text;
            });
            $grid->column('value');
            $grid->column('carousel_time');
            $grid->column('override_reward_rate')->display(function () {

                $rate = $this->adplatform->reward_rate;
                if ($this->override_reward_rate > 0) {
                    $rate = $this->override_reward_rate;
                }
                return $rate;
            });
            $grid->column('status')->using([
                Enum::STATUS_ENABLED => '启用',
                Enum::STATUS_DISABLED => '禁用',
            ])->label([
                Enum::STATUS_ENABLED => 'success',
                Enum::STATUS_DISABLED => 'danger',
            ])->sortable();
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->setActionClass(Grid\Displayers\Actions::class);


            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Admodel(), function (Show $show) {
            $show->field('id');
            $show->field('adtype_id')->as(function($id){
                return Adtype::find($id)->name ?? '未知类型';
            });
            $show->field('adplatform_id')->as(function($id){
                return Adplatforms::find($id)->name ?? '未知类型';
            });
            $show->field('value');
            $show->field('carousel_time');
            $show->field('override_reward_rate');
            $show->field('status')->using([
                Enum::STATUS_ENABLED => '启用',
                Enum::STATUS_DISABLED => '禁用',
            ]);
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Admodel(), function (Form $form) {
            $form->display('id');
            $form->select('adtype_id')
                ->options(Adtype::query()->where(['status' => Enum::STATUS_ENABLED])->pluck('name', 'id'))
                ->required();
            $form->select('adplatform_id')
                ->options(Adplatforms::query()->where(['status' => Enum::STATUS_ENABLED])->pluck('name', 'id'))
                ->required();
            $form->text('value')->required();
            $form->text('carousel_time')->default(0)->required();
            $form->text('override_reward_rate')->help('如果填写了会覆盖广告类型的设置，0.0 到 1.0 之间');
            $form->radio('status')->options(Enum::getSelectableStatusExplains())->default(Enum::STATUS_ENABLED)->required();

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
